package LongestAbsoluteFilePath;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution {
	class File {
		private String filename;
		private int level;
		private int aLength;

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}



		public int getaLength() {
			return aLength;
		}

		public void setaLength(int aLength) {
			this.aLength = aLength;
		}

		public int getLevel() {
			return level;
		}

		public void setLevel(int level) {
			this.level = level;
		}

	}

	public int lengthLongestPath(String input) {
		int index = 0;
		int result = 0;
		
		Stack<File> stack = new Stack<File>();
		while (index < input.length()) {
			Boolean isSystem = false;
			StringBuilder temp = new StringBuilder();
			char ch = input.charAt(index);
			int level = 0;
			while (index < input.length() && ch == '\t') {
				level++;
				index++;
				if (index >= input.length())
					break;
				ch = input.charAt(index);
			}

			while (index < input.length() && ch != '\n') {
				if (ch == '.')
					isSystem = true;
				temp.append(ch);
				index++;
				if (index >= input.length())
					break;
				ch = input.charAt(index);
			}
			index++;
			File file = new File();
			file.setFilename(temp.toString());
			/*System.out.println(file.getFilename());*/
			file.setLevel(level);
			file.setaLength(temp.length());
			if (isSystem) result = Math.max(result, file.getaLength());

			while (stack.isEmpty() == false) {
				File upFile = stack.peek();
				if (upFile.level == level - 1) {
					file.setaLength(file.getaLength() + upFile.aLength + 1);
					if (file.getaLength() > result && isSystem)
						result = file.getaLength();
					stack.push(file);
					break;
				} else
					stack.pop();
			}
			if (stack.isEmpty())
				stack.push(file);

		}

		return result;
	}

	public static void main(String args[]) {
		String t = "a";
		Solution so = new Solution();
		System.out.println(so.lengthLongestPath(t));
	}
}
